package Tydzien8;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


public class MyApplet extends Applet implements ItemListener, ActionListener {

	private static final long serialVersionUID = 9194254163642555734L;
	
	CheckboxGroup checkboxGroup;
	Checkbox[] checkbox;
	Choice choice;
    TextField text1;
    TextField text2;
    Button button1, button2;
    MenuFrame fullmenuWindow;
    Image image;

    public void init()
    {
    	
    	image = getImage(getCodeBase(), "welcome.jpg"); 
    	
    	checkboxGroup = new CheckboxGroup();
    	checkbox = new Checkbox[5];
    	for(int i = 0; i < 5; ++i)
    	{
    		checkbox[i] = new Checkbox(Integer.toString(i + 1), false, checkboxGroup);
    		add(checkbox[i]);
            checkbox[i].addItemListener(this);
    	}

        text1 = new TextField(20);
        add(text1);
        text1.setEditable(false);
        
        choice = new Choice();
        add(choice);
        choice.add("Pozycja 1");
        choice.add(getParameter("selection2"));
        choice.add(getParameter("selection3"));
        choice.add(getParameter("selection4"));
        choice.addItemListener(this);
        
        text2 = new TextField(20);
        add(text2);
        text2.setEditable(false); 
        
        button1 = new Button("Show full menu window");
        add(button1);
        button1.addActionListener(this);
        button2 = new Button("Hide full menu window");
        add(button2);
        button2.addActionListener(this);

        fullmenuWindow = new MenuFrame("Full menus");
        fullmenuWindow.setSize(100, 100);
    }

    public void itemStateChanged(ItemEvent e) 
    {
    	for(int i = 0; i < 5; ++i)
    	{
    		if(e.getItemSelectable() == checkbox[i])
        	{
    			text1.setText("Checkbox " + (i + 1) + " clicked!");
    			break;
        	}
    	}
    	
    	if(e.getItemSelectable() == choice)
    	{
    		text2.setText(((Choice)e.getItemSelectable()).getSelectedItem());
    	}
    }

	public void paint (Graphics g) { 
		g.drawLine(100, 60, 10, 210); 
		g.drawLine(10, 210, 150, 210); 
		g.drawLine(150, 210, 100, 60); 
		// Powieksza rozmiar okna appletu 
		setSize(500,550); // albo takze funkcja resize(400,400); 
		// zmienia kolor linii
		g.setColor(Color.blue); 
		// rysuje kwadrat lub prostokat 
		g.drawRect(10,260,120,120); 
		g.drawRect(20,270,100,100); 
		g.drawRect(30,280,80,80); 
		// rysuje kwadrat lub prostokat o zaokraglonych bokach 
		g.setColor(Color.red); 
		g.drawRoundRect(100, 350, 70, 70, 30, 30);
		// rysuje elipse wpisana w podany prostokat 
		g.setColor(Color.MAGENTA); 
		g.drawOval(250, 100, 80, 40); 
		// Pobiera obrazek z pliku i wyswietla go w podanym prostokacie 
		g.drawImage(image, 210, 250, 276, 145, this);
		 
		// Ustawia kroj i parametry czcionki w applecie 
		Font appFont = new Font("Times New Roman", Font.PLAIN, 22); 
		g.setColor(Color.blue); 
		g.setFont(appFont); 
		g.drawString("Hello World !!!", 175, 500); 
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getSource() == button1)
		{
            fullmenuWindow.setVisible(true);
		}
		if(event.getSource() == button2)
		{
            fullmenuWindow.setVisible(false);
		}
	} 
}
